#include "mywrod.h"

//统一图表来源
const QString strPath = ":/images";

MyWrod::MyWrod(QWidget *parent)
    : QMainWindow(parent)
{

    mdiArea = new QMdiArea;
    //1.在内容太大而无法容纳时显示滚动条
    mdiArea->setHorizontalScrollBarPolicy(Qt::ScrollBarAsNeeded);
    mdiArea->setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded);
    //2.把mdiArea设置到中心部件里来
    this->setCentralWidget(mdiArea);
    //3.连接信号与槽，mdiArea被激活，调用槽函数updateMenus()
    connect(mdiArea,SIGNAL(subWindowActivated(QMdiSubWindow*)),this,SLOT(updateMenus()));
    //4.创建映射mapper
    windowMapper = new QSignalMapper(this);
    //5.连接信号与槽，发出map()信号时,调用槽函数setActiveSubWindow()
    connect(windowMapper,SIGNAL(mapped(QWidget *)),this,SLOT(setActiveSubWindow(QWidget*)));
    //6.创建菜单、工具栏、状态栏等相关操作(动作集合)
    createActions();
    //7.调用创建菜单实现
    createMenus();
    //8.调用创建工具条
    createToolBars();
    //9.创建状态条
    createStatusBar();

    resize(900,600);
    setWindowIcon(QIcon(strPath + "/wordlogo2.png"));
    setWindowTitle(tr("Office办公自动化文字编辑软件 v1.0"));
    //窗口的标题栏和工具栏合并
    setUnifiedTitleAndToolBarOnMac(true);


}

MyWrod::~MyWrod()
{
}


void MyWrod::closeEvent(QCloseEvent *event)
{
    //尝试关闭所有子窗口
    mdiArea->closeAllSubWindows();

    //如果仍然有子窗口未关闭
    if(mdiArea->currentSubWindow())
    {
        //阻止主窗口关闭
        event->ignore();
    }
    else
    {
        //允许主窗口关闭
        event->accept();
    }
}

//创建菜单(动作)
void MyWrod::createActions()
{
    //1.文件菜单动作
    //新建文件
    newAct = new QAction(QIcon(strPath + "/filenew.png"),tr("新建(&N)"),this);
    //设置快捷键
    newAct->setShortcut(Qt::CTRL + Qt::Key_N);
    //设置工具栏按钮提示文本信息
    newAct->setToolTip(tr("新建"));
    //设置状态栏提示信息
    newAct->setStatusTip(tr("创建一个新的word文档."));
    //连接信号与槽，newAct被触发，调用fileNew()槽函数
    connect(newAct,SIGNAL(triggered()),this,SLOT(fileNew()));

    //打开文件
    openAct = new QAction(QIcon(strPath + "/fileopen.png"),tr("打开(&O)"),this);
    openAct->setShortcut(Qt::CTRL + Qt::Key_O);
    openAct->setToolTip(tr("打开"));
    openAct->setStatusTip(tr("打开已经存在的word文档."));
    connect(openAct,SIGNAL(triggered()),this,SLOT(fileOpen()));

    //保存文件
    saveAct = new QAction(QIcon(strPath + "/filesave.png"),tr("保存(&S)"),this);
    saveAct->setShortcut(Qt::CTRL + Qt::Key_S);
    saveAct->setToolTip(tr("保存"));
    saveAct->setStatusTip(tr("将当前的word文档存盘."));
    connect(saveAct,SIGNAL(triggered()),this,SLOT(fileSave()));

    //另存为文件
    saveAsAct = new QAction(tr("另存为(&A)..."),this);
    saveAsAct->setShortcut(Qt::CTRL + Qt::Key_A);
    saveAsAct->setToolTip(tr("另存为"));
    saveAsAct->setStatusTip(tr("用另一个新的名称，保存当前word文档."));
    connect(saveAsAct,SIGNAL(triggered()),this,SLOT(fileSaveAs()));

    //打印
    printAct = new QAction(QIcon(strPath + "/fileprint.png"),tr("打印(&P)"),this);
    printAct->setShortcut(Qt::CTRL + Qt::Key_P);
    printAct->setToolTip(tr("打印"));
    printAct->setStatusTip(tr("打印当前word文档."));
    connect(printAct,SIGNAL(triggered()),this,SLOT(printPrint()));

    //打印预览
    printPreviewAct = new QAction(tr("打印预览..."),this);
    printPreviewAct->setToolTip(tr("打印预览"));
    printPreviewAct->setStatusTip(tr("打印预览当前word文档效果."));
    connect(printPreviewAct,SIGNAL(triggered()),this,SLOT(filePrintPreview()));

    //退出操作
    exitAct = new QAction(tr("退出(X)"),this);
    exitAct->setShortcuts(QKeySequence::Quit);
    exitAct->setToolTip(tr("退出"));
    exitAct->setStatusTip(tr("退出Word文档应用程序"));
    connect(exitAct,SIGNAL(triggered()),qApp,SLOT(closeAllWindows()));

    //2.编辑菜单动作
    //撤销
    undoAct = new QAction(QIcon(strPath + "/editundo.png"),tr("撤销(&U)"),this);
    undoAct->setShortcut(Qt::CTRL + Qt::Key_P);
    undoAct->setToolTip(tr("撤销"));
    undoAct->setStatusTip(tr("撤销当前Word文档操作."));
    connect(undoAct,SIGNAL(triggered()),this,SLOT(undo()));

    //重做
    redoAct = new QAction(QIcon(strPath + "/editredo.png"),tr("重做(&R)"),this);
    redoAct->setShortcut(Qt::CTRL + Qt::Key_R);
    redoAct->setToolTip(tr("重做"));
    redoAct->setStatusTip(tr("恢复之前的Word文档操作."));
    connect(redoAct,SIGNAL(triggered()),this,SLOT(redo()));

    //剪切
    cutAct = new QAction(QIcon(strPath + "/editcut.png"),tr("剪切(&T)"),this);
    cutAct->setShortcut(Qt::CTRL + Qt::Key_X);
    cutAct->setToolTip(tr("重做"));
    cutAct->setStatusTip(tr("剪切."));
    connect(cutAct,SIGNAL(triggered()),this,SLOT(cut()));

    //复制
    copyAct = new QAction(QIcon(strPath + "/editcopy.png"),tr("复制(&C)"),this);
    copyAct->setShortcut(Qt::CTRL + Qt::Key_C);
    copyAct->setToolTip(tr("复制"));
    copyAct->setStatusTip(tr("复制所选中的Word内容，将他放到剪切板."));
    connect(copyAct,SIGNAL(triggered()),this,SLOT(copy()));
    //粘贴
    pasteAct = new QAction(QIcon(strPath + "/editpaste.png"),tr("粘贴(&P)"),this);
    pasteAct->setShortcut(Qt::CTRL + Qt::Key_V);
    pasteAct->setToolTip(tr("粘贴"));
    pasteAct->setStatusTip(tr("将剪切板中的内容粘贴到当前word文档."));
    connect(pasteAct,SIGNAL(triggered()),this,SLOT(paste()));

    //3.[格式菜单] -->字体动作
    //加粗
    boldAct = new QAction(QIcon(strPath + "/textbold.png"),tr("加粗(&B)"),this);
    boldAct->setCheckable(true);
    boldAct->setShortcut(Qt::CTRL + Qt::Key_B);
    boldAct->setToolTip(tr("加粗"));
    boldAct->setStatusTip(tr("将你所选择的文字进行加粗处理."));

    QFont bold;
    bold.setBold(true);
    boldAct->setFont(bold);
    connect(boldAct,SIGNAL(triggered()),this,SLOT(textBold()));

    //倾斜
    italicAct = new QAction(QIcon(strPath + "/textitalic.png"),tr("倾斜(&I)"),this);
    italicAct->setCheckable(true);
    italicAct->setShortcut(Qt::CTRL + Qt::Key_I);
    italicAct->setToolTip(tr("倾斜"));
    italicAct->setStatusTip(tr("将你所选择的文字进行倾斜处理."));

    QFont italic;
    italic.setItalic(true);
    italicAct->setFont(italic);
    connect(italicAct,SIGNAL(triggered()),this,SLOT(textItalic()));


    //下划线
    underlineAct = new QAction(QIcon(strPath + "/textunder.png"),tr("下划线(&U)"),this);
    underlineAct->setCheckable(true);
    underlineAct->setShortcut(Qt::CTRL + Qt::Key_U);
    underlineAct->setToolTip(tr("下划线"));
    underlineAct->setStatusTip(tr("将你所选择的文字添加下划线."));
    QFont underline;
    underline.setUnderline(true);
    underlineAct->setFont(underline);
    connect(underlineAct,SIGNAL(triggered()),this,SLOT(textUnderline()));

    //4.[格式菜单] -->段落动作
    //子菜单下各项为同一个菜单项组，我们只能选中其中的一项
    QActionGroup *actGroup = new QActionGroup(this);
    connect(actGroup,SIGNAL(triggered(QAction*)),this,SLOT(textAlign(QAction*)));

    //检测系统是否使用从左到右的布局
    if(QApplication::isLeftToRight())
    {
        leftAlignAct = new QAction(QIcon(strPath + "/textleft.png"),tr("左对齐(&L)"),actGroup);
        centerAct = new QAction(QIcon(strPath + "/textcenter.png"),tr("居中对齐(&E)"),actGroup);
        rightAlignAct = new QAction(QIcon(strPath + "/textright.png"),tr("右对齐(&R)"),actGroup);
    }
    else
    {
        rightAlignAct = new QAction(QIcon(strPath + "/textright.png"),tr("右对齐(&R)"),actGroup);
        centerAct = new QAction(QIcon(strPath + "/textcenter.png"),tr("居中对齐(&E)"),actGroup);
        leftAlignAct = new QAction(QIcon(strPath + "/textleft.png"),tr("左对齐(&L)"),actGroup);
    }
    justifyAct = new QAction(QIcon(strPath + "/textjustify.png"),tr("两端对齐(&J"),actGroup);

    leftAlignAct->setShortcut(Qt::CTRL + Qt::Key_L);
    leftAlignAct->setCheckable(true);
    leftAlignAct->setToolTip(tr("左对齐"));
    leftAlignAct->setStatusTip(tr("将选择文字进行左对齐."));

    centerAct->setShortcut(Qt::CTRL + Qt::Key_E);
    centerAct->setCheckable(true);
    centerAct->setToolTip(tr("居中对齐"));
    centerAct->setStatusTip(tr("将选择文字进行居中对齐."));

    rightAlignAct->setShortcut(Qt::CTRL + Qt::Key_L);
    rightAlignAct->setCheckable(true);
    rightAlignAct->setToolTip(tr("右对齐"));
    rightAlignAct->setStatusTip(tr("将选择文字进行右对齐."));

    justifyAct->setShortcut(Qt::CTRL + Qt::Key_J);
    justifyAct->setCheckable(true);
    justifyAct->setToolTip(tr("两端对齐"));
    justifyAct->setStatusTip(tr("将选择文字进行两端对齐."));

    //创建了一个 16x16 像素的空白图像
    QPixmap pix(16,16);
    //将这个图像填充为纯红色，最终得到一个红色的小方块图标
    pix.fill(Qt::red);

    colorAct = new QAction(pix,tr("颜色(&C)..."),this);
    colorAct->setToolTip("颜色");
    colorAct->setStatusTip(tr("将你选择的文字设置对应的颜色."));
    connect(colorAct,SIGNAL(triggered()),this,SLOT(textColor()));


    //4.[窗口菜单]

    //关闭
    closeAct = new QAction(tr("关闭(&O)"),this);
    closeAct->setStatusTip(tr("关闭活动word文档子窗口."));
    //closeActiveSubWindow() 是系统实现的，关闭子窗口
    connect(closeAct,SIGNAL(triggered()),mdiArea,SLOT(closeActiveSubWindow()));


    //关闭所有
    closeAllAct = new QAction(tr("关闭所有(&A)"),this);
    closeAllAct->setStatusTip(tr("关闭所有活动word文档子窗口."));
    //closeAllSubWindows() 是系统实现的，关闭所有子窗口
    connect(closeAllAct,SIGNAL(triggered()),mdiArea,SLOT(closeAllSubWindows()));

    //平铺子窗口
    tileAct = new QAction(tr("平铺(&A)"),this);
    tileAct->setStatusTip(tr("平铺所有活动word文档子窗口."));
    //tileSubWindows() 是系统实现的，平铺所有子窗口
    connect(tileAct,SIGNAL(triggered()),mdiArea,SLOT(tileSubWindows()));

    //层叠子窗口
    cascadeAct = new QAction(tr("层叠(&T)"),this);
    cascadeAct->setStatusTip(tr("层叠所有活动word文档子窗口."));
    //cascadeSubWindows() 是系统实现的，层叠所有子窗口
    connect(cascadeAct,SIGNAL(triggered()),mdiArea,SLOT(cascadeSubWindows()));

    //下一个子窗口
    nextAct = new QAction(tr("下一个(&T)"),this);
    nextAct->setShortcuts(QKeySequence::NextChild);
    nextAct->setStatusTip(tr("移动焦点到下一个子窗口."));
    //activateNextSubWindow() 是系统实现的，移动焦点到下一个子窗口
    connect(nextAct,SIGNAL(triggered()),mdiArea,SLOT(activateNextSubWindow()));


    //上一个子窗口
    previousAct = new QAction(tr("前一个(&V)"),this);
    previousAct->setShortcuts(QKeySequence::PreviousChild);
    previousAct->setStatusTip(tr("移动焦点到前一个子窗口."));
    //activatePreviousSubWindow() 是系统实现的，移动焦点到前一个子窗口
    connect(previousAct,SIGNAL(triggered()),mdiArea,SLOT(activatePreviousSubWindow()));

    separatorAct = new QAction(this);
    separatorAct->setSeparator(true);

    //5.[帮助菜单]
    //关于
    aboutAct = new QAction(tr("关于(&A)"),this);
    aboutAct->setStatusTip(tr("关于Office Word相关信息."));
    connect(aboutAct,SIGNAL(triggered()),this,SLOT(about()));

    //关于Qt
    aboutQtAct = new QAction(tr("关于Qt(&Qt)"),this);
    aboutQtAct->setStatusTip(tr("关于Qt库相关信息."));
    connect(aboutQtAct,SIGNAL(triggered()),this,SLOT(aboutQt()));
}

//创建菜单
void MyWrod::createMenus()
{
    //1.文件菜单
    fileMenu = menuBar()->addMenu(tr("文件(&F)"));
    fileMenu->addAction(newAct);
    fileMenu->addAction(openAct);
    fileMenu->addSeparator();//分隔符
    fileMenu->addAction(saveAct);
    fileMenu->addAction(saveAsAct);
    fileMenu->addSeparator();//分隔符
    fileMenu->addAction(printAct);
    fileMenu->addAction(printPreviewAct);
    fileMenu->addAction(exitAct);

    //2.编辑菜单
    editMenu = menuBar()->addMenu(tr("编辑(&E)"));
    editMenu->addAction(undoAct);
    editMenu->addAction(redoAct);
    editMenu->addSeparator();
    editMenu->addAction(cutAct);
    editMenu->addAction(copyAct);
    editMenu->addAction(pasteAct);

    //3.格式菜单
    formatMenu = menuBar()->addMenu(tr("格式(&O)"));
    fontMenu = formatMenu->addMenu(tr("字体(&D)"));//字体子菜单
    fontMenu->addAction(boldAct);

    fontMenu->addAction(italicAct);
    fontMenu->addAction(underlineAct);


    alignMenu = formatMenu->addMenu(tr("段落"));//段落子菜单
    alignMenu->addAction(leftAlignAct);
    alignMenu->addAction(centerAct);
    alignMenu->addAction(rightAlignAct);
    alignMenu->addAction(justifyAct);

    formatMenu->addAction(colorAct);

    //4.窗口菜单
    windowMenu = menuBar()->addMenu(tr("窗口(&W)"));
    updateWindowMenu();
    connect(windowMenu,SIGNAL(aboutToShow()),this,SLOT(updateWindowMenu()));
    windowMenu->addSeparator();

    //5.帮助菜单
    helpMenu = menuBar()->addMenu(tr("帮助(&H)"));
    helpMenu->addAction(aboutAct);
    helpMenu->addSeparator();
    helpMenu->addAction(aboutQtAct);
}

//创建工具条
void MyWrod::createToolBars()
{
    //1.文件
    fileToolBar = addToolBar(tr("文件"));
    fileToolBar->addAction(newAct);
    fileToolBar->addAction(openAct);
    fileToolBar->addAction(saveAct);
    fileToolBar->addSeparator();//分隔符
    fileToolBar->addAction(printAct);

    //2.编辑
    editToolBar = addToolBar(tr("编辑"));
    editToolBar->addAction(undoAct);
    editToolBar->addAction(redoAct);
    editToolBar->addAction(cutAct);
    editToolBar->addAction(copyAct);
    editToolBar->addAction(pasteAct);
    //3.格式
    formatToolBar = addToolBar(tr("格式"));
    formatToolBar->addAction(boldAct);
    formatToolBar->addAction(italicAct);
    formatToolBar->addAction(underlineAct);
    formatToolBar->addSeparator();//分隔符
    formatToolBar->addAction(leftAlignAct);
    formatToolBar->addAction(centerAct);
    formatToolBar->addAction(rightAlignAct);
    formatToolBar->addAction(justifyAct);
    formatToolBar->addSeparator();//分隔符
    formatToolBar->addAction(colorAct);
    //4.组合工具栏
    //编号
    addToolBarBreak(Qt::TopToolBarArea);//工具栏换行
    comboToolBar = addToolBar(tr("组合选择"));
    comboStyle = new QComboBox();
    comboToolBar->addWidget(comboStyle);
    comboStyle->addItem("标准");
    comboStyle->addItem("标准");
    comboStyle->addItem("项目符号 (●)");
    comboStyle->addItem("项目符号 (○)");
    comboStyle->addItem("项目符号 (■)");
    comboStyle->addItem("编号 (⒈⒉⒊)");
    comboStyle->addItem("编号 ( a.b.c.)");
    comboStyle->addItem("编号 ( A.B.C.)");
    comboStyle->addItem("编号 (ⅰ.ⅱ.ⅲ.)");
    comboStyle->addItem("编号 (Ⅰ.Ⅱ.Ⅲ.)");
    comboStyle->setStatusTip("段落加标号或编号");
    connect(comboStyle, SIGNAL(activated(int)), this, SLOT(textStyle(int)));

    //字体
    comboFont = new QFontComboBox();
    comboToolBar->addWidget(comboFont);
    comboFont->setStatusTip("更改字体");
    connect(comboFont,SIGNAL(activated(QString)),this,SLOT(textFamily(QString)));

    //字体大小
    comboSize = new QComboBox();
    comboToolBar->addWidget(comboSize);
    comboSize->setEditable(true);
    comboSize->setStatusTip("更改字号");

    QFontDatabase db;
    foreach(int size,db.standardSizes())
    {
        comboSize->addItem(QString::number(size));
    }
    connect(comboSize,SIGNAL(activated(QString)),this,SLOT(textSize(QString)));
}

void MyWrod::createStatusBar()
{
     statusBar()->showMessage(tr("就绪状态"));
}

//使得[格式]下的各个子菜单可用
void MyWrod::enabledText()
{
    boldAct->setEnabled(true);
    italicAct->setEnabled(true);
    underlineAct->setEnabled(true);
    leftAlignAct->setEnabled(true);
    centerAct->setEnabled(true);
    rightAlignAct->setEnabled(true);
    justifyAct->setEnabled(true);
    colorAct->setEnabled(true);
}

//字体更改
void MyWrod::fontChanged(const QFont &f)
{

    //QFontInfo(f).family())获取目标字体的名称
    comboFont->setCurrentIndex(comboFont->findText(QFontInfo(f).family()));
    comboSize->setCurrentIndex(comboSize->findText(QString::number(f.pointSize())));
    boldAct->setChecked(f.bold());
    italicAct->setChecked(f.italic());
    underlineAct->setChecked(f.underline());
}

//改变小方框颜色
void MyWrod::colorChanged(const QColor &c)
{
    QPixmap pix(16,16);
    pix.fill(c);
    colorAct->setIcon(pix);
}

//根据当前文本的对齐方式，更新界面上对齐按钮的勾选状态
void MyWrod::alignmentChanged(Qt::Alignment a)
{
    if(a & Qt::AlignLeft)
    {
        leftAlignAct->setChecked(true);
    }
    else if(a & Qt::AlignCenter)
    {
        centerAct->setChecked(true);
    }
    else if(a & Qt::AlignRight)
    {
        rightAlignAct->setChecked(true);
    }
    else if(a & Qt::AlignJustify)
    {
        justifyAct->setChecked(true);
    }
}

//激活子窗口
MyChild *MyWrod::activeMyChild()
{
    QMdiSubWindow *activeSubWindow;
    if(activeSubWindow = mdiArea->activeSubWindow());
    {
        return qobject_cast<MyChild*>(activeSubWindow->widget());
    }
    return 0;
}

//文件使用
QMdiSubWindow *MyWrod::findMyChild(const QString &fileName)
{
    QString canonicalFilePath = QFileInfo(fileName).canonicalFilePath();
    foreach(QMdiSubWindow *window,mdiArea->subWindowList())
    {
        MyChild *myChild = qobject_cast<MyChild*>(window->widget());
        if(myChild->currentFile() == canonicalFilePath)
        {
            return window;
        }
    }
    return 0;
}

MyChild *MyWrod::createMyChild()
{
    MyChild *child = new MyChild;
    mdiArea->addSubWindow(child);
    //当孩子里有文字被选中时（copyAvailable信号触发），就会自动激活 剪切 和 复制
    connect(child,SIGNAL(copyAvailable(bool)),cutAct,SLOT(setEnabled(bool)));
    connect(child,SIGNAL(copyAvailable(bool)),copyAct,SLOT(setEnabled(bool)));
    return child;
}

//新建
void MyWrod::fileNew()
{
    MyChild *child = createMyChild();
    child->newFile();
    child->show();
    enabledText();//使得字体设置菜单可用的
}


//打开
void MyWrod::fileOpen()
{
    QString fileName = QFileDialog::getOpenFileName(this,"打开",
                                                    QString(),tr("HTML 文档(*.htm *.html);;所有文件(*.*)"));
    if(!fileName.isEmpty())
    {
        QMdiSubWindow *existing = findMyChild(fileName);
        if(existing)
        {
            mdiArea->setActiveSubWindow(existing);
            return;
        }
        MyChild *child = createMyChild();
        if(child->loadFile(fileName))
        {
            statusBar()->showMessage(tr("文件已经加载"),2000);
            child->show();
            enabledText();
        }
        else
        {
            child->close();
        }

    }
}

//保存
void MyWrod::fileSave()
{
    if(activeMyChild() && activeMyChild()->save())
    {
        statusBar()->showMessage(tr("word文档保存成功."),2000);
    }
}

//另存为
void MyWrod::fileSaveAs()
{
    if(activeMyChild() && activeMyChild()->saveAs())
    {
        statusBar()->showMessage(tr("word文档另存为成功."),2000);
    }
}

//打印
void MyWrod::filePrint()
{
    QPrinter printer(QPrinter::HighResolution);
    QPrintDialog *pdlg = new QPrintDialog(&printer,this);
    if(activeMyChild()->textCursor().hasSelection())
    {
        pdlg->addEnabledOption(QAbstractPrintDialog::PrintSelection);
    }
    pdlg->setWhatsThis(tr("打印文档"));
    if(pdlg->exec()==QDialog::Accepted)
    {
        activeMyChild()->print(&printer);
    }
    delete pdlg;
}


//打印预览
void MyWrod::filePrintPreview()
{

}



//撤销
void MyWrod::undo()
{
    if(activeMyChild())
    {
        activeMyChild()->undo();
    }
}

//重做
void MyWrod::redo()
{
    if(activeMyChild())
    {
        activeMyChild()->redo();
    }
}

//剪切
void MyWrod::cut()
{
    if(activeMyChild())
    {
        activeMyChild()->cut();
    }
}

//复制
void MyWrod::copy()
{
    if(activeMyChild())
    {
        activeMyChild()->copy();
    }
}

//粘贴
void MyWrod::paste()
{
    if(activeMyChild())
    {
        activeMyChild()->paste();
    }
}

//加粗
void MyWrod::textBold()
{
    QTextCharFormat fmt;
    fmt.setFontWeight(boldAct->isChecked() ? QFont::Bold : QFont::Normal);
    if(activeMyChild())
    {
        activeMyChild()->mergeFormationOnWordOrSelection(fmt);
    }
}

//倾斜
void MyWrod::textItalic()
{
    QTextCharFormat fmt;
    fmt.setFontItalic(italicAct->isChecked());
    if(activeMyChild())
    {
        activeMyChild()->mergeFormationOnWordOrSelection(fmt);
    }
}

//下划线
void MyWrod::textUnderline()
{
    QTextCharFormat fmt;
    fmt.setFontUnderline(underlineAct->isChecked());
    if(activeMyChild())
    {
        activeMyChild()->mergeFormationOnWordOrSelection(fmt);
    }
}


//对齐
void MyWrod::textAlign(QAction *a)
{
    if(activeMyChild())
    {
        if(a == leftAlignAct)
        {
            activeMyChild()->setAligin(1);
        }
        else if(a == centerAct)
        {
            activeMyChild()->setAligin(2);
        }
        else if(a == rightAlignAct)
        {
            activeMyChild()->setAligin(3);
        }
        else if(a == justifyAct)
        {
            activeMyChild()->setAligin(4);
        }

    }
}

//文本编号样式
void MyWrod::textStyle(int styleIndex)
{
    if(activeMyChild())
    {
        activeMyChild()->setStyle(styleIndex);
    }
}

//字体样式
void MyWrod::textFamily(const QString &f)
{
    QTextCharFormat fmt;
    fmt.setFontFamily(f);
    if(activeMyChild())
    {
        activeMyChild()->mergeFormationOnWordOrSelection(fmt);
    }
}

//字体大小
void MyWrod::textSize(const QString &p)
{
    qreal pointsize = p.toFloat();
    if(p.toFloat()>0)
    {
        QTextCharFormat fmt;
        fmt.setFontPointSize(pointsize);
        if(activeMyChild())
        {
            activeMyChild()->mergeFormationOnWordOrSelection(fmt);
        }
    }
}

//字体颜色
void MyWrod::textColor()
{
    if(activeMyChild())
    {
        QColor color = QColorDialog::getColor(activeMyChild()->textColor(),this);
        if(!color.isValid())
        {
            return;
        }
        QTextCharFormat fmt;
        fmt.setForeground(color);
        activeMyChild()->mergeFormationOnWordOrSelection(fmt);
        colorChanged(color);
    }
}

void MyWrod::about()
{
    QMessageBox::about(this,tr("关于"),tr("此软件是基于Qt5实现的一个文字处理软件!!!"));
}

void MyWrod::aboutQt()
{
    QMessageBox::aboutQt(this);
}

void MyWrod::updateMenus()
{
//    //至少有一个子文档打开着的情况
//    bool hasMyChild = (activeMyChild()!=0);

//    saveAct->setEnabled(hasMyChild);
//    saveAsAct->setEnabled(hasMyChild);
//    printAct->setEnabled(hasMyChild);
//    printPreviewAct->setEnabled(hasMyChild);

//    pasteAct->setEnabled(hasMyChild);

//    closeAct->setEnabled(hasMyChild);
//    closeAllAct->setEnabled(hasMyChild);
//    tileAct->setEnabled(hasMyChild);
//    cascadeAct->setEnabled(hasMyChild);
//    nextAct->setEnabled(hasMyChild);
//    previousAct->setEnabled(hasMyChild);
//    separatorAct->setVisible(hasMyChild);


//    //文档打开着并且其中有内容被选中的情况
//    bool hasSelection = (activeMyChild() && activeMyChild()->textCursor().hasSelection());

//    cutAct->setEnabled(hasSelection);
//    copyAct->setEnabled(hasSelection);

//    boldAct->setEnabled(hasSelection);
//    italicAct->setEnabled(hasSelection);
//    underlineAct->setEnabled(hasSelection);
//    leftAlignAct->setEnabled(hasSelection);
//    centerAct->setEnabled(hasSelection);
//    rightAlignAct->setEnabled(hasSelection);
//    justifyAct->setEnabled(hasSelection);
//    colorAct->setEnabled(hasSelection);
}

//根据当前打开的子窗口状态，实时刷新“窗口”菜单的内容（如关闭、排列、切换子窗口等选项）
void MyWrod::updateWindowMenu()
{
    //1.清空并重建菜单
    windowMenu->clear();
    windowMenu->addAction(closeAct);
    windowMenu->addAction(closeAllAct);
    windowMenu->addSeparator();
    windowMenu->addAction(tileAct);
    windowMenu->addAction(cascadeAct);
    windowMenu->addSeparator();
    windowMenu->addAction(nextAct);
    windowMenu->addAction(previousAct);

    //2.获取子窗口列表,并根据是否有子窗口（!windows.isEmpty()），控制分隔线（separatorAct）的显示/隐藏。
    QList<QMdiSubWindow*> windows = mdiArea->subWindowList();
    separatorAct->setVisible(!windows.isEmpty());

    //3.动态添加子窗口项
    for(int i=0;i<windows.size();i++)
    {
        //从 MDI（多文档界面）子窗口中获取其内部嵌入的 实际控件（Widget）
        MyChild *child = qobject_cast<MyChild*>(windows.at(i)->widget());
        QString text;
        if(i<9)
        {
            text=tr("&%1 %2").arg(i+1).arg(child->userFriendlyCurrentFile());
        }
        else
        {
            text=tr("%1 %2").arg(i+1).arg(child->userFriendlyCurrentFile());

        }

        QAction *action = windowMenu->addAction(text);
        action->setCheckable(true);
        //通过 setChecked(true) 高亮当前活动的子窗口（activeMyChild()）
        action->setChecked(child == activeMyChild());
        //点击子窗口项时，通过 QSignalMapper（windowMapper）触发窗口切换逻辑
        connect(action,SIGNAL(triggered()),windowMapper,SLOT(map()));
        windowMapper->setMapping(action,windows.at(i));
    }

    enabledText();//使得字体菜单可用
}

//设置active激活窗口
void MyWrod::setActiveSubWindow(QWidget *window)
{
    if(!window)
    {
        return ;
    }
    mdiArea->setActiveSubWindow(qobject_cast<QMdiSubWindow*>(window));
}

